package com.xxyf.service.imp;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.xxyf.entity.Like;
import com.xxyf.mapper.LikeMapper;
import com.xxyf.mapper.MessageMapper;
import com.xxyf.mapper.ReplyMapper;
import com.xxyf.service.LikeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Objects;
import java.util.Set;
import java.util.stream.Collectors;

/**
*@Author 小小怡飞
*@Date 2023/3/2 23:54
*@Version JDK 8
*/
@Service
public class LikeImp implements LikeService {
    @Autowired
    private RedisTemplate<String,Object> redisTemplate;
    @Autowired
    private LikeMapper likeMapper;
    @Autowired
    private MessageMapper messageMapper;
    @Autowired
    private ReplyMapper replyMapper;
    @Transactional
    @Override
    public void updata() {
//       先更新回复表
        Set<String> keys = redisTemplate.keys(MessageImp.LIKE +":"+MessageImp.REPLY+ ":*");
        updataLike(MessageImp.REPLY,replyMapper,keys);
//      再更新留言表
        Set<String> key = redisTemplate.keys(MessageImp.LIKE +":"+MessageImp.REPLY+ ":*");
        updataLike(MessageImp.MSG,messageMapper,key);

    }
    private <T> void updataLike(String type, BaseMapper<T> baseMapper,Set<String> key){
        if (key!=null && key.size()>0){
            for (String x : key) {
//                获取表idd
                String id = x.substring(x.lastIndexOf(":") + 1);
                UpdateWrapper<T> queryWrapper = new UpdateWrapper<>();
                //                更新表自动加入点赞数据
                queryWrapper.eq("id",id).set("star",redisTemplate.opsForSet().size(x));

                baseMapper.update(null,queryWrapper);
//                获取该字段点赞用户列表
                Set<Object> members = redisTemplate.opsForSet().members(x);
                if (members!=null && members.size()>0){

                    List<Integer> collect = members.stream().map(x1 -> ((Integer) x1)).collect(Collectors.toList());
                    collect.forEach(itme->{
//                        构建like对象
                        Like like = new Like();
                        like.setUserid(Long.valueOf(itme));
//                        根据条件更新
                        if (Objects.equals(type,MessageImp.REPLY)){
                            like.setReplyid(Long.valueOf(id));
                        }else {
                            like.setMsgid(Long.valueOf(id));
                        }
//                        插入前先查询是否存在
                        LambdaQueryWrapper<Like> wrapper = new LambdaQueryWrapper<>();
                        wrapper.eq(Like::getUserid,like.getUserid());
                        wrapper.eq(Like::getMsgid,like.getMsgid());
                        wrapper.eq(Like::getReplyid,like.getReplyid());
                        Like like1 = likeMapper.selectOne(wrapper);
//                        不存在即可插入
                        if (like1==null){
                            likeMapper.insert(like);
                        }
                    });
                }
            }
        }


    }

    public static void main(String[] args) {
        String string = "like:rer:124";
        String substring = string.substring(string.lastIndexOf(":")+1);
        System.out.println(substring);
    }
}
